$(function(){ 
	init();
}); 
function init(){
	$(document).keydown(function(event){ 
		if(event.keyCode==13){ 
			var username = $("#username").val();
			 var password = $("#password").val();
			 if(username === null || username === ""){
			    	alert("请输入用户名！");
			    	return;
			 }
			 console.log(password);
			 if(password === null || password === ""){
			    	alert("请输入密码！");
			    	return;
			 }
			 $.ajax({
		    	 type : "post",
		         url : "/TeaCulture/login/do",
		         data : { "username" : username , "password" : password },
		         dataType : "json",
		         traditional : true,
		         success : function(data){
		        	 if(data.result.code == 1){
		        		 var storage=window.localStorage;
		                 storage.setItem("count",data.onlineuserCount);
		        		 alert(data.user.nickname+"登录成功!");
		        		
		        		 window.location.href="main?nickname="+encodeURI(encodeURI(data.user.nickname))+"&usersequence="+data.user.sequence+"";
		        	 }else{
		        		 alert("登录失败"+data.result.des);
		        	 }
		        	
		            	
		        },    
		        error : function(data){
		            alert("登录失败"+data.result.des);
		        }
		    });
		} 
	}); 
	$("#username").change(function(){
		var username = $("#username").val();
		$.ajax({
	    	 type : "post",
	         url : "/TeaCulture/login/check",
	         data : { "username" : username },
	         dataType : "json",
	         traditional : true,
	         success : function(data){
	        	 if(data.result.code == 1){
	        		 $("#userImg").attr('src',data.user.img);
	        	 }
	        	
	            	
	        },    
	        error : function(){
	        	
	        }
	    });
	});
	
	$("#login").on('click',function(){
		 var username = $("#username").val();
		 var password = $("#password").val();
		 if(username === null || username === ""){
		    	alert("请输入用户名！");
		    	return;
		 }
		 console.log(password);
		 if(password === null || password === ""){
		    	alert("请输入密码！");
		    	return;
		 }
		 $.ajax({
	    	 type : "post",
	         url : "/TeaCulture/login/do",
	         data : { "username" : username , "password" : password },
	         dataType : "json",
	         traditional : true,
	         success : function(data){
	        	 if(data.result.code == 1){
	        		 var storage=window.localStorage;
	                 storage.setItem("count",data.onlineuserCount);
	        		 alert(data.user.nickname+"登录成功!");
	        		 window.location.href="main?nickname="+encodeURI(encodeURI(data.user.nickname))+"&usersequence="+data.user.sequence+"";
	        	 }else{
	        		 alert("登录失败"+data.result.des);
	        	 }
	        	
	            	
	        },    
	        error : function(data){
	            alert("登录失败"+data.result.des);
	        }
	    });
	});
}